package org.dromara.admin.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.admin.enums.AccountBookType;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.admin.domain.vo.AccountBookVo;
import org.dromara.admin.domain.bo.AccountBookBo;
import org.dromara.admin.service.IAccountBookService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 后台账本
 *
 * @author Lion Li
 * @date 2025-09-23
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/accountBook")
public class AdminAccountBookController extends BaseController {

    private final IAccountBookService accountBookService;

    /**
     * 查询账本列表
     */
//    @SaCheckPermission("all:accountBook:list")
    @GetMapping("/list")
    public TableDataInfo<AccountBookVo> list(AccountBookBo bo, PageQuery pageQuery) {
        return accountBookService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询账本中租金收入列表(结算记录)
     */
//    @SaCheckPermission("all:accountBook:list")
    @GetMapping("/rentList")
    public TableDataInfo<AccountBookVo> rentList(AccountBookBo bo, PageQuery pageQuery) {
        bo.setType(AccountBookType.RENT_INCOME.getCode());
        return accountBookService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出账本列表
     */
//    @SaCheckPermission("all:accountBook:export")
    @Log(title = "账本", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AccountBookBo bo, HttpServletResponse response) {
        List<AccountBookVo> list = accountBookService.queryList(bo);
        ExcelUtil.exportExcel(list, "账本", AccountBookVo.class, response);
    }

    /**
     * 获取账本详细信息
     *
     * @param id 主键
     */
//    @SaCheckPermission("all:accountBook:query")
    @GetMapping("/{id}")
    public R<AccountBookVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(accountBookService.queryById(id));
    }

    /**
     * 新增账本
     */
//    @SaCheckPermission("all:accountBook:add")
    @Log(title = "账本", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AccountBookBo bo) {
        return toAjax(accountBookService.insertByBo(bo));
    }

    /**
     * 修改账本
     */
//    @SaCheckPermission("all:accountBook:edit")
    @Log(title = "账本", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AccountBookBo bo) {
        return toAjax(accountBookService.updateByBo(bo));
    }

    /**
     * 删除账本
     *
     * @param ids 主键串
     */
//    @SaCheckPermission("all:accountBook:remove")
    @Log(title = "账本", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(accountBookService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 后台审核资方的提现
     */
    @Log(title = "后台审核提现", businessType = BusinessType.UPDATE)
    @PutMapping("/withdrawalReview")
    public R<Void> withdrawalReview(@RequestBody AccountBookBo bo) {
        return toAjax(accountBookService.withdrawalReview(bo));
    }
}
